#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gconf/gconf-client.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>

#include "totem-uri.h"
#include "video-utils.h"
#include "totem-plugin.h"
#include "totem-private.h"
#include "totem.h"
#include "totem-sidebar.h"
#include "totem-playlist.h"
#include "bacon-video-widget.h"

#include "lmp-debug.h"
#include "totem-lrc.h"

#include "totem-lrc-down.h"
#include "totem-lrc-func.h"
#include "totem-lrc-pad.h"

static void totem_lrc_plugin_init		(TotemLrcPlugin *plugin);
static gboolean impl_activate		(TotemPlugin *plugin, TotemObject *totem, GError **error);
static void impl_deactivate		(TotemPlugin *plugin, TotemObject *totem);
static gboolean totem_lrc_plugin_set_stuff(TotemLrcPlugin *plugin);
static void
totem_lrc_plugin_set_lyrics(TotemLrcPlugin *plugin);
static
void totem_lrc_plugin_set_lrcpad(TotemPlugin *plugin);

TOTEM_PLUGIN_REGISTER(TotemLrcPlugin, totem_lrc_plugin)

const gchar *def_code[] ={"cp936","UTF-16", NULL};

enum{
	SIGNAL_EVT_ID,
	SIGNAL_IDS
};

///static int Lrc_siganl[SIGNAL_IDS]={0};

const gchar *lrc_keys[PLUGIN_LAST_KEYS+1]={NULL};


static void
totem_lrc_plugin_class_init(TotemLrcPluginClass *klass)
{
	lmp_debug("");
	TotemPluginClass *plugin_class = TOTEM_PLUGIN_CLASS (klass);
	GObjectClass *object_class = (GObjectClass *) klass;

	plugin_class->activate = impl_activate;
	//when totem active plugin ,impl_activate can be called.

	plugin_class->deactivate = impl_deactivate;
	//when totem deactive plugin ,impl_deactivate can be called.
/*
	Lrc_siganl[SIGNAL_EVT_ID] = g_signal_new("evt_id",
	                             G_TYPE_FROM_CLASS (object_class),
	                             G_SIGNAL_RUN_LAST,
	                             G_STRUCT_OFFSET (TotemLrcPluginClass, lrc_evt_id),
	                             NULL,NULL,
	                             g_cclosure_marshal_VOID__CHAR,
	                             G_TYPE_NONE,1,G_TYPE_CHAR);
*/
	plugin_class->create_configure_dialog = dialog_config;
	lmp_debug("inited");
}


static void
totem_lrc_plugin_init (TotemLrcPlugin *plugin)
{
	lmp_debug("");
	plugin->default_code = def_code;
	plugin->win = NULL;
	plugin->pad = NULL;
	plugin->totem = NULL;
	totem_lrc_keys(); /* init the array */
}

/*
 * A thread for searching lyrics,now it is do with ttplayer server
 * In future,It is will do with more server(like baidu)  
 */

gpointer totem_lrc_plugin_search_thread(TotemLrcPlugin *pi)
{
	tt_free_list(pi->minfo->list);
	lmp_debug("pi->minfo->get_lyric_list:%p",pi->minfo->get_lyric_list);
	pi->minfo->list = NULL;
	if(!pi->minfo->get_lyric_list){
		pi->minfo->get_lyric_list = tt_get_lyrics_list;
	}
	pi->minfo->list = pi->minfo->get_lyric_list(pi->minfo->artist,pi->minfo->title);
	lmp_debug("");
///	print_list(pi->minfo->list);
	if(pi->minfo->list){
		if(TASK_GO_SEARCH == pi->tsk)
			totem_lrc_plugin_send_event(pi,TASK_READY_DOWNLOAD);
		else
			totem_lrc_plugin_send_event(pi,TASK_SEARCH_OK);
	}else{
		totem_lrc_plugin_send_event(pi,TASK_SEARCH_FAIL);
	}
	return NULL;
}

gpointer totem_lrc_plugin_down_thread(TotemLrcPlugin *pi)
{
	if(!pi->minfo->url){
		totem_lrc_plugin_send_event(pi,TASK_DOWNLOAD_FAIL);
	}else{
		if(tt_get_lyrics_content_save(pi->minfo->url,
		                              pi->minfo->lyricfile_w)){
			totem_lrc_plugin_send_event(pi,TASK_DOWNLOAD_OK);
		}else{
			totem_lrc_plugin_send_event(pi,TASK_DOWNLOAD_FAIL);
		}
	}
	return NULL;
}

static
gboolean totem_lrc_timeout_func (TotemLrcPlugin *pi)
{
	GDK_THREADS_ENTER ();
	free_minfo(pi->minfo);
	totem_lrc_plugin_set_stuff(pi);

	totem_lrc_pad_set_text(pi->pad,pi->minfo->title);
	if(!pi->minfo->path){
		lmp_debug("Can't do lyric shows");
		return ;
	}
	totem_lrc_plugin_set_lyrics(pi);
	if(pi->timeout_id){
		g_source_remove(pi->timeout_id);
		pi->timeout_id = 0;
	}
	totem_lrc_plugin_send_event(pi,TASK_SEARCH_AUTO);
	pi->timeout_id = 0;
	GDK_THREADS_LEAVE ();
	return FALSE;
}

void totem_lrc_plugin_send_event(TotemLrcPlugin *pi,gint id)
{
	pi->tsk = id ;
	gchar *data = g_strdup_printf("%d\n",id);
	fprintf(pi->filew,data);
	fflush(pi->filew);
	lmp_debug("send %s",data);
	g_free(data);
}

static
gboolean totem_lrc_plugin_on_event(GIOChannel *source,
                                   GIOCondition condition,
                                   TotemLrcPlugin *pi)
{
	gchar *line = NULL;
	GIOStatus status;
	GError *err = NULL;

	status = g_io_channel_read_line (source, &line, NULL, NULL, NULL);
	if (status != G_IO_STATUS_NORMAL)
		return FALSE;
	gint64 event_id = g_ascii_strtoll(line,NULL,0);
	lmp_debug("Got %s",line);
	g_free(line);
	gboolean res = TRUE;
	switch(event_id){
	case TASK_DOWNLOAD_OK:
		lmp_debug("download ok !");
		if(g_access(pi->minfo->lyricfile_w,R_OK)==0 
		  && g_strcmp0(pi->minfo->lyricfile_w,pi->minfo->lyricfile_r )){
			g_free(pi->minfo->lyricfile_r);
			pi->minfo->lyricfile_r = pi->minfo->lyricfile_w;
		  }
		totem_lrc_pad_load_lrc(pi->pad);
		totem_lrc_pad_run(pi->pad);
		totem_lrc_pad_response(pi->pad,DIALOG_DOWNLOAD);
	break;
	case TASK_SEARCH_OK:
		if(!pi->minfo->list){
			break;
		}
		LyricId *id = pi->minfo->list->data;
		g_free(pi->minfo->url);
		pi->minfo->url = id->url?g_strdup(id->url):NULL;
	case TASK_GO_DOWNLOAD:
		///MUST SET pi->minfo->url before send this
		g_thread_create(totem_lrc_plugin_down_thread,pi,FALSE,&err);
		if(err){
			lmp_debug("g_thread_create():%s",err->message);
			g_error_free(err);
			err = NULL;
		}
		lmp_debug("search ok !");
	break;
	case TASK_READY_DOWNLOAD:
		lmp_debug("Lyric found ,ready to download .");
		totem_lrc_pad_response(pi->pad,DIALOG_SEARCH);
	break;
	case TASK_GO_SEARCH:
		g_thread_create(totem_lrc_plugin_search_thread,pi,FALSE,&err);
		if(err){
			lmp_debug("g_thread_create():%s",err->message);
			g_error_free(err);
			err = NULL;
		}
	break;
	case TASK_SEARCH_AUTO:
		if(pi->minfo->lyricfile_r){
			totem_lrc_plugin_send_event(pi, TASK_DOWNLOAD_OK);
		}else if (pi->minfo->lyricfile_w){
			g_thread_create(totem_lrc_plugin_search_thread,pi,FALSE,&err);
			if(err){
				lmp_debug("g_thread_create():%s",err->message);
				g_error_free(err);
				err = NULL;
			}
		}else{
			lmp_debug("totem-lrc will not lyric things .");
		}
	break;
	case TASK_SEARCH_FAIL:
		totem_lrc_pad_response(pi->pad,DIALOG_SEARCH);
///		totem_lrc_pad_set_text(TOTEM_LRC_PAD(pi->pad),_("Failed to search lyrics"));
		lmp_debug("search fail !");
	break;
	case TASK_DOWNLOAD_FAIL:
///		totem_lrc_pad_set_text(TOTEM_LRC_PAD(pi->pad),_("Failed to download lyrics"));
		totem_lrc_pad_response(pi->pad,DIALOG_DOWNLOAD);
		lmp_debug("download fail !");
	break;
	case TASK_NOTHING:
		res = FALSE;
	break;
	default:
	break;
	}
	return res;
}

void totem_lrc_plugin_set_artist_title(TotemLrcPlugin *plugin)
{
	Totem *totem = plugin->totem;
	char *artist = NULL;
	char *title = NULL;
	MataInfo *m = plugin->minfo ;
	GValue value = { 0, };
	
	/* get artist */
	bacon_video_widget_get_metadata (totem->bvw, BVW_INFO_ARTIST/*BVW_INFO_TITLE*/, &value);
	char *tmp = g_value_dup_string (&value);
	g_value_unset (&value);

	gchar *tmp1 = encode_back_to_originally(tmp);
	g_free(tmp);
	artist = totem_lrc_plugin_encode_to_utf8(plugin,tmp1);
	g_free(tmp1);

	/* get title */
///	lmp_debug("geting title");
	bacon_video_widget_get_metadata (totem->bvw, /*BVW_INFO_ARTIST*/BVW_INFO_TITLE, &value);
	tmp = g_value_dup_string (&value);
	g_value_unset (&value);

	gboolean strip_flag = FALSE;
	if(!tmp){
		tmp = totem_get_short_title(totem);
		strip_flag = TRUE;
	}

//	lmp_debug("%s",tmp);
	tmp1 = encode_back_to_originally(tmp);
	g_free(tmp);
	title = totem_lrc_plugin_encode_to_utf8(plugin,tmp1);
	g_free(tmp1);
	if(title&&strip_flag){
		gint len = strlen(title)-1;
		gint i = len;
		while(i>1&&i>len-5){
			if(title[i]=='.'){
				title[i] = 0;
				break;
			}
			i--;
		}
	}
	g_free(m->artist);
	g_free(m->title);
	m->artist = artist;
	m->title = title ;
	lmp_debug("minfo->artist:%s",m->artist);
	lmp_debug("minfo->title:%s",m->title);
}

/*
 * set struct ,artist ,title ,path ...
 */
static gboolean totem_lrc_plugin_set_stuff(TotemLrcPlugin *plugin)
{
	lmp_debug("");
	lmp_debug("minfo");
	Totem *totem = plugin->totem;
	MataInfo *m = plugin->minfo ;
	char *path = NULL ;

	if(!totem){
		lmp_debug("plugin->totem must be seted .");
		return FALSE;
	}

	totem_lrc_plugin_set_artist_title(plugin);
	if(totem->mrl&&strncmp("file://",totem->mrl,strlen("file://"))==0){
		/* deal with local file only */
		path =  g_filename_from_uri(totem->mrl,NULL,NULL);
	}else{
		path = NULL;
	}
	g_free(m->path);
	m->path = path ;
	return TRUE;
}

/*
 * load saved setting to a GKeyFile 
 */
void
totem_lrc_plugin_load_conf(TotemLrcPlugin *plugin)
{
	lmp_debug("");
	char *conf = NULL;
	GError *err = NULL;
	GKeyFile *key = plugin->conf;
	if(!key){
		key = g_key_file_new();
	}
	conf = g_build_filename(g_get_home_dir(),".totem_lrcconf",NULL);
	if(g_access(conf,R_OK)==0){
		g_key_file_load_from_file(key,
		                          conf,
		                          G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS,
		                          &err);
	}else{
		g_key_file_set_string(key,
		                      lrc_keys[PLUGIN_NAME],
		                      lrc_keys[PLUGIN_LRC_PATHS],
		                      ".;");
	}
	g_free(conf);
	if(err){
		lmp_debug("g_key_file_load_from_file():%s .",err->message);
		g_error_free(err);
		err = NULL;
	}
	plugin->conf = key;
}

/*
 * Save settings to configuration files
 */
void
totem_lrc_plugin_save_conf(TotemLrcPlugin *plugin)
{
	lmp_debug("");
	char *conf = NULL;
//	GError *err = NULL;
	GKeyFile *key = plugin->conf;
	FILE *fp = NULL;
	if(!key){
		return ;
	}
	conf = g_build_filename(g_get_home_dir(),".totem_lrcconf",NULL);
	fp = fopen(conf,"w");
	g_free(conf);
	if(!fp){
		lmp_debug("fopen():%s .",strerror(errno));
		return ;
	}
	gsize len = 0;
	gchar *data = g_key_file_to_data(key,&len,NULL);
	if(data&&len>0){
		fwrite(data,len,1,fp);
	}else{
		lmp_debug("g_key_file_to_data(key,&len,NULL):error");
	}
	g_free(data);
	fclose(fp);
}

/*
 * Set lyric files to read and write
 */
static void
totem_lrc_plugin_set_lyrics(TotemLrcPlugin *plugin)
{
	lmp_debug("");
	GKeyFile *key = NULL;
	gchar *lrc_r = NULL;
	gchar *lrc_w = NULL;
	MataInfo *m = plugin->minfo ;
	gchar **path_list = NULL;
	key = plugin->conf ;

	if(m->lyricfile_w == m->lyricfile_r){
		g_free(m->lyricfile_r);
	}else{
		g_free(m->lyricfile_r);
		g_free(m->lyricfile_w);
	}
	m->lyricfile_r = NULL;
	m->lyricfile_w = NULL;

	if(!key)
		return ;

	if(!m->path)
		return ;

	path_list = g_key_file_get_string_list(key,
	                                      lrc_keys[PLUGIN_NAME],
	                                      lrc_keys[PLUGIN_LRC_PATHS],
	                                      NULL,
	                                      NULL);
	if(!path_list){  /* for g_strfreev */
		path_list = g_new0(char *,2);
		path_list[0] = g_strdup(".");
		path_list[1] = NULL;
		g_key_file_set_string_list(key,
		                           lrc_keys[PLUGIN_NAME],
		                           lrc_keys[PLUGIN_LRC_PATHS],
		                           path_list,
		                           1);
	}
	gint i = 0;
	gchar *filedir = g_path_get_dirname(m->path);
	gchar *basename = g_path_get_basename(m->path);
	gchar *item ;
	while(path_list[i]){
		item = path_list[i];
		i++;
		gchar *h;
		if(*item == '.'){
			item = filedir;
		}
		gboolean h_flag ;
		h_flag  = FALSE;
		h = g_strdup_printf("%s/%s.lrc",item,basename);
		if(!lrc_r){
			if(g_access(h,R_OK)==0){
				lrc_r = h;
				h_flag = TRUE;
			}else{
				gchar *j = g_strdup_printf("%s/%s_%s.lrc",item,m->artist,m->title);
//				lmp_debug(j);
				if(g_access(j,R_OK)==0){
					lrc_r = j;
				}else{
					g_free(j);
				}
			}
		}
		if(!lrc_w){
			if(lrc_r&&g_access(lrc_r,W_OK)==0){
				lrc_w = lrc_r;
			}else{
				if(g_access(item,W_OK)==0){
					lrc_w = h;
					h_flag = TRUE;
				}
			}
		}
		if(!h_flag)
			g_free(h_flag);
		if(lrc_r && lrc_w)
			break;
	}
	m->lyricfile_r = lrc_r;
	m->lyricfile_w = lrc_w;
	lmp_debug("lrc_w:%s",lrc_w);
	lmp_debug("lrc_r:%s",lrc_r);
	g_free(filedir);
	lmp_debug("free(filedir):%p",filedir);
	g_free(basename);
	lmp_debug("free(basename):%p",basename);
	g_strfreev(path_list);
}

/*
 * When open a file ,Totem get a signal named "file-opened"
*/
static void
totem_lrc_plugin_file_opened (TotemObject *totem,
					   const char *mrl,
					   TotemLrcPlugin *plugin)
{
	TotemLrcPlugin *pi = TOTEM_LRC_PLUGIN(plugin);
	if(pi->timeout_id){
		g_source_remove(pi->timeout_id);
		pi->timeout_id = 0;
	}
	pi->timeout_id = g_timeout_add(1000,totem_lrc_timeout_func,pi);
}

/*
 * When close a file ,Totem get a signal named"file_closed"
*/
static void
totem_lrc_plugin_file_closed (TotemObject *totem,
					   TotemLrcPlugin *plugin)
{
	lmp_debug("***closed run***");
	TotemLrcPlugin *pi = TOTEM_LRC_PLUGIN(plugin); 
	if(pi->timeout_id){
		g_source_remove(pi->timeout_id);
		pi->timeout_id = 0;
	}
	TOTEM_LRC_PAD(pi->pad)->lts = LRC_STOP;
	lmp_debug("***closed return***");
}


static void
totem_lrc_plugin_mata(TotemObject *totem,
				const char *artist, 
				const char *title, 
				const char *album,
				guint track_num,
				TotemLrcPlugin *plugin)
{
	lmp_debug("metadata-updated");
	g_signal_stop_emission_by_name(totem,"metadata-updated");
	totem_lrc_plugin_file_opened(totem,NULL,plugin);
	totem_lrc_plugin_send_event(TOTEM_LRC_PLUGIN( plugin),TASK_DOING);
}

/*
 * Init lyric show  things in this function
 * it can be specify in 'totem_lrc_plugin_class_init'
*/
static gboolean
impl_activate (TotemPlugin *plugin,
	       TotemObject *totem,
	       GError **error)
{
	lmp_debug("");
	TotemLrcPlugin *pi;
	gint pipefd[2];

	pi = TOTEM_LRC_PLUGIN (plugin);
	pi->totem = totem;
	pi->timeout_id = 0;
	if(!pi->minfo)
		pi->minfo = g_new0(MataInfo,1);
	pi->pad = totem_lrc_pad_new(pi);

	totem_lrc_plugin_load_conf(pi);

	pipe(pipefd);
	GIOChannel *gio = g_io_channel_unix_new(pipefd[0]);
	pi->filew = fdopen(pipefd[1],"w");
	g_io_add_watch(gio,G_IO_IN|G_IO_PRI|G_IO_HUP,totem_lrc_plugin_on_event,pi);
	g_io_channel_unref (gio);

	if(totem_is_playing(totem)||totem_is_paused(totem)){
		totem_lrc_plugin_file_opened(totem,NULL,pi);
	}

	totem_lrc_plugin_set_lrcpad(plugin);

///	totem_add_sidebar_page (totem, "lrcshow", _("LrcShow"), pi->pad);

	/* Have to handle signals
	 * or will not Totem's current state changed
	 */

	pi->open_id = g_signal_connect (G_OBJECT (totem),
							"file-opened",
							G_CALLBACK (totem_lrc_plugin_file_opened),
							plugin);

	pi->close_id = g_signal_connect (G_OBJECT (totem),
							"file-closed",
							G_CALLBACK (totem_lrc_plugin_file_closed),
							plugin);

	pi->mata_id = g_signal_connect_after (G_OBJECT (totem),
								"metadata-updated",
								G_CALLBACK (totem_lrc_plugin_mata),
								plugin);

//	g_signal_connect(pi,"evt_id",G_CALLBACK(totem_lrc_plugin_on_evt_id),"Hi");
	return TRUE;
}

/*
 * Deal things When don't this plugin
 * specify it in 'totem_lrc_plugin_class_init'
*/
static void
impl_deactivate	(TotemPlugin *plugin,
		 TotemObject *totem)
{
	lmp_debug("");
	TotemLrcPlugin *pi;
	pi = TOTEM_LRC_PLUGIN (plugin);

	/* disconnect signal handlers */
	g_signal_handler_disconnect (G_OBJECT (totem), pi->open_id);
	g_signal_handler_disconnect (G_OBJECT (totem), pi->close_id);
	g_signal_handler_disconnect (G_OBJECT (totem), pi->mata_id);

	TOTEM_LRC_PAD(pi->pad)->lts = LRC_STOP;

	totem_lrc_plugin_save_conf(pi);
	free_minfo(pi->minfo);
	g_free(pi->minfo);
	pi->minfo = NULL;
	totem_lrc_plugin_send_event(pi,TASK_NOTHING);
	totem_remove_sidebar_page(totem,lrc_keys[PLUGIN_SIDE_BAR]);

	if(pi->win)
		gtk_widget_destroy (pi->win);
	fclose(pi->filew);
}

gboolean totem_lrc_plugin_get_win_model(TotemLrcPlugin *pi)
{
	return g_key_file_get_boolean(pi->conf,lrc_keys[PLUGIN_NAME],lrc_keys[PLUGIN_SIDE_BAR],NULL);
}

void totem_lrc_plugin_set_win_model(TotemLrcPlugin *pi,gboolean model)
{
	g_key_file_set_boolean(pi->conf,lrc_keys[PLUGIN_NAME],lrc_keys[PLUGIN_SIDE_BAR],model);
}

static
void totem_lrc_plugin_set_lrcpad(TotemPlugin *plugin)
{
	TotemLrcPlugin *pi;
	pi = TOTEM_LRC_PLUGIN (plugin);
	gboolean model = totem_lrc_plugin_get_win_model(pi);
	lmp_debug("");
	if(model){
		lmp_debug("lyric show in a signal window");
		g_object_ref(pi->pad);
		if(!pi->win){
			totem_remove_sidebar_page (pi->totem,lrc_keys[PLUGIN_SIDE_BAR]);
			g_object_ref(pi->pad);
		}
		pi->win = totem_lrc_plugin_get_win(pi);
		gtk_widget_show_all(GTK_WIDGET(pi->win));
	}else{
		lmp_debug("lyric show in side");
		if(pi->win){
			gtk_container_remove(pi->win,pi->pad);
			g_object_ref(pi->pad);///这行必须如此
			gtk_widget_destroy(pi->win);
			pi->win = NULL;
		}
		totem_add_sidebar_page (pi->totem, lrc_keys[PLUGIN_SIDE_BAR], _("LrcShow"), pi->pad);
		totem_sidebar_set_current_page(pi->totem,lrc_keys[PLUGIN_SIDE_BAR],TRUE);
	}
	lmp_debug("exit .");
}

void on_button_ok(GtkButton *button,ConfigData *cdata)
{
	lmp_debug("cdata->pi->conf:%p",cdata->pi->conf);
	const char *lrcdir = gtk_entry_get_text(GTK_WIDGET(cdata->widget));
	g_key_file_set_value(cdata->pi->conf,lrc_keys[PLUGIN_NAME],lrc_keys[PLUGIN_LRC_PATHS],lrcdir);
	GtkWidget *top = gtk_widget_get_toplevel(GTK_WIDGET(button));
	gtk_widget_destroy(top);
	totem_lrc_plugin_save_conf(cdata->pi);
	lmp_debug("");
}

void on_button_add(GtkButton *button,ConfigData *cdata)
{
	GtkWidget *dialog;

	dialog = gtk_file_chooser_dialog_new (_("Select Folder"),
				      gtk_widget_get_toplevel(GTK_WIDGET(button)),
				      GTK_FILE_CHOOSER_ACTION_OPEN|GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
				      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				      NULL);

	gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_CENTER_ON_PARENT);

	gchar *dirname = g_path_get_dirname(cdata->pi->minfo->path);
	lmp_debug("dirname:%s",dirname);
	if(dirname){
		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),dirname);
		g_free(dirname);
	}
	dirname = NULL;

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT){
		char *filename;
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		const gchar *olddirs = gtk_entry_get_text(GTK_ENTRY(cdata->widget));
		gchar *newdirs = g_strdup_printf("%s%s;",olddirs,filename);
		g_free (filename);
		gtk_entry_set_text(GTK_ENTRY(cdata->widget),newdirs);
		g_free(newdirs);
	}
	gtk_widget_destroy (dialog);
	lmp_debug("return");
}

void on_button_chk(GtkToggleButton *togglebutton,TotemPlugin *plugin)
{
	gboolean flag = gtk_toggle_button_get_active(togglebutton);
	totem_lrc_plugin_set_win_model(TOTEM_LRC_PLUGIN(plugin),flag);
	totem_lrc_plugin_set_lrcpad(plugin);
}

/*
 * configure plugin 
 */

GtkWidget * dialog_config(TotemPlugin *plugin)
{
	TotemLrcPlugin *pi = TOTEM_LRC_PLUGIN(plugin);
	GtkWidget *dialog  = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	GtkWidget *vbox = gtk_vbox_new(FALSE,2);
	GtkWidget *entry = gtk_entry_new();
	GtkWidget *hbox = gtk_hbox_new(FALSE,2);
	GtkWidget *chkbutton;
	static ConfigData cdata = {NULL};

	GtkWidget *browser = gtk_button_new_from_stock(GTK_STOCK_ADD);
	GtkWidget *button = gtk_button_new_from_stock(GTK_STOCK_OK);
	chkbutton = gtk_check_button_new_with_mnemonic(_("Use a sigal window ."));
	GtkWidget *label = gtk_label_new(_("The path to save lrc files:"));

	gtk_misc_set_alignment(GTK_MISC(label),0,0);

	gchar *dirs = g_key_file_get_value(pi->conf,lrc_keys[PLUGIN_NAME],lrc_keys[PLUGIN_LRC_PATHS],NULL);
	gtk_entry_set_text(GTK_ENTRY(entry),dirs);
	g_free(dirs);

	cdata.pi = pi;
	cdata.widget = entry;

	gboolean flag = totem_lrc_plugin_get_win_model(TOTEM_LRC_PLUGIN(plugin));
	gtk_toggle_button_set_active(chkbutton,flag);

	gtk_box_pack_start(GTK_BOX(hbox),entry,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(hbox),browser,FALSE,TRUE,0);

	gtk_box_pack_start(GTK_BOX(vbox),label,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(vbox),hbox,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(vbox),chkbutton,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,0);

	gtk_container_add(GTK_CONTAINER(dialog),vbox);
	gtk_window_set_title(GTK_WINDOW(dialog),_("Configure"));
	gtk_window_set_resizable (GTK_WINDOW(dialog),FALSE);

	g_signal_connect(button,"clicked",G_CALLBACK(on_button_ok),&cdata);
	g_signal_connect(browser,"clicked",G_CALLBACK(on_button_add),&cdata);
	g_signal_connect(chkbutton,"toggled",G_CALLBACK(on_button_chk),plugin);

//	gtk_widget_set_parent(dialog,GTK_WIDGET(totem_get_main_window(lrc_totem)));
	gtk_window_set_position(GTK_WINDOW(dialog),///GTK_WIN_POS_MOUSE
	GTK_WIN_POS_CENTER);
	gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog),TRUE);
	gtk_widget_show_all(dialog);

	return dialog;
}
